
<?php echo CHtml::beginForm(); ?>

<?php

$cs = Yii::app()->clientScript;


$this->Widget('application.extensions.jsTree.CjsTree', array (
	'htmlOptions' => array (
		'id' => 'project_topic_tree'
	),
	//'ui' => array (
	//	'theme_name' => 'checkbox'
	//),
	//'options' => array (
	//	'plugins' => array (
	//		'checkbox' => array (
	//			'three_state' => 'true'
	//		)
	//	)
	//),
	'genDiv' => false,
	'callback' => array (
		'onload' => "js:function() { \n" .
		"}"
	)
));

echo CHtml::activeHiddenField($model, 'project_id') . "\n";
echo CHtml::activeHiddenField($model, 'id') . "\n";
$form_name = 'TopicValidationForm[topics]';
?>

<h2><?php echo Yii::t('project', 'Others validated items'); ?></h2>
<div class="yiiForm" id="project_topic">
	<?php echo CHtml::errorSummary($model); ?>

	<div id="project_topic_tree">
	<?php


	displaySteps($this, $form_name, $model->steps);
	?>
	</div><!-- project_topic_tree -->
</div><!-- yiiForm -->

<div class="action">
<?php echo CHtml::submitButton(Yii::t('views', '< Previous'), array('name' => 'prev_button')); ?>
<?php echo CHtml::submitButton(Yii::t('views', 'Next >'), array('name' => 'next_button')); ?>
</div>

<?php

echo CHtml::endForm();

function displaySteps($p, $model, $steps) {
	if (is_array($steps) && count($steps) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($steps as $step) {
				echo "<li id='"."node_" . $step->id . "' class='open node' style='width:100%;'>\n";
				echo "\t<a href='#'>" . $step->label . "</a>";
				displayTopics($p, $model, $step->topics);
				echo "</li>";
		}
		echo "</ul>";
	}
}

function displayTopics($p, $model, $topics) {
	$image = "";

	if (is_array($topics) && count($topics) > 0) {
		echo "<ul style='width:100%;'>";
		foreach ($topics as $topic) {
				$image = CHtml::image('images/arrow_down_blue.png', 'Show/Hide validation comments', array (
					'id' => 'img_comment_' . $topic->project_topic_id
				));
				echo "<li id='node_" . $topic->project_topic_id . "' class='node'>\n" .
				"\t\n\t$image\n\t" .
				"\n\t<a href='#'>" . $topic->label . "</a>";
					$p->widget('adviser.widgets.CommentLayer', array (
						'topic_id' => $topic->project_topic_id,
						'columns' => 60,
						'rows' => 5,
						'prefix' => '',
						'adviser_validation' => $topic->topic->adviser_validation,
						'adviser_topic_validation' => $topic->topic->adviser_validation_topic,
						'owner_validation' => $topic->topic->owner_validation,
						'owner_topic_validation' => $topic->topic->owner_validation_topic,
						'comment' => $topic->topic->validation_comments,
						'form_name' => $model
					));
				displayTopics($p, $model, $topic->children);
				echo "</li>";
		}
		echo "</ul>";
	}
}

$script =<<<JS
init_topics($("#project_topic_tree"));
$("#project_topic_tree").parent().append('<hr class="separation" />');
$("#project_topic_tree a:xhEdtIcon").bind("dblclick.jstree",function(event){
	event.preventDefault();
	event.stopPropagation();
	return false; }
)
.bind("click.jstree",function(event){
	event.preventDefault();
	event.stopPropagation();
	return false; }
)
.parent("not:node").parent("not:node").parent("not:node")
.removeClass("tree-default")
.removeClass("tree-checkbox");
JS;

$cs->registerScript('topics_form_save', $script);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/masks.js', CClientScript::POS_HEAD);
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/topic_validation.js', CClientScript::POS_HEAD);
?>
